/**
 * 空对象检测方法
 */
const isEmptyObj = (obj) => {
	if (typeof(obj) != "object") return false;

	return Object.keys(obj).length > 0 ? false : true;
}

/**
 * 当前组件依赖：
 * 1、data.swiperItemHeights array
 * 2、data.swiperHeight int 内部Swiper的高度
 * 使用：
 * beforeMount() 进行绑定
 */
const updateSwiperHeight = (_this, height) => {
	let swiperItemHeights = _this.swiperItemHeights;
	swiperItemHeights.push(height);
	let maxHeight = 0;
	swiperItemHeights.forEach(ele => {
		if (ele > maxHeight) {
			maxHeight = ele;
		}
	})
	_this.swiperHeight = maxHeight + 12;
	_this.swiperItemHeights = swiperItemHeights;
	for(let i in _this.$refs) {
		_this.$refs[i].swiperHeight = maxHeight;
	}
}

/**
 * 当前组件依赖：
 * 1、data.swiperHeight int 内部Swipter的高度
 * 2、props.parent 父组件的引用
 * 使用：
 * beforeMount() 进行绑定
 * mounted() 逐个进行获取高度
 */
const getSwiperMaxHeight = (_this, selectorId='.swiper-item-box') => {
	let height = _this.swiperHeight;
	let query = uni.createSelectorQuery().select(selectorId);
	query.boundingClientRect(function(data) {
		if (data && data.height > height) {
			height = data.height;
		}
	}).exec();
	_this.swiperHeight = height;
	_this.parent.updateSwiperHeight(_this.parent, height);
}

/**
 * json 转 url的query 不带"?"
 * json 为一维的json对象
 */
const buildHttpQuery = obj => {
	let query = "";
	for (i in obj) {
	    query += "&" + i + "=" + obj[i]
	}
	
	return query.substr(1);
}

module.exports = {
	isEmptyObj: isEmptyObj,
	updateSwiperHeight: updateSwiperHeight,
	getSwiperMaxHeight: getSwiperMaxHeight,
	buildHttpQuery: buildHttpQuery
}
